<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
        $mainframe = JFactory::getApplication();
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_town.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    /**
	*Get data table PREF add in pulldown
	*/
    function getPREF(){
        $this->_db->setQuery("SELECT PREF_CODE, PREF_NAME FROM pref");
        return $this->_db->loadObjectList();
    }
    
    /**
	*Get Total
	*/
    function getTotal($where = null){
        //Load the content if it doesn't already exist
        if($this->_total === null){
            $query = "SELECT COUNT(DISTINCT TOWN_CODE) FROM town LEFT JOIN ocba ON OCBA_TOWN_CODE = TOWN_CODE $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        return $this->_total;
    }
    
    /**
	*Get Pagination
	*/
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    /**
	*Get data table TOWN add in list
	*/
    function getTown($u_ocba = null){
        $keyword = JRequest::getVar('keyword');
        $pref = JRequest::getVar('pref');
        
        $sql = "SELECT DISTINCT TOWN_CODE,TOWN_NAME,TOWN_POPU,TOWN_ADD1,TOWN_FLAG,TOWN_WEBF,TOWN_DBSY 
                FROM town 
                LEFT JOIN ocba ON OCBA_TOWN_CODE = TOWN_CODE ";

        $where = array();
       if(!empty($keyword)){
            $where[] = "TOWN_CODE LIKE '%$keyword%' OR TOWN_NAME LIKE '%$keyword%'";
       }
       if(!empty($pref)){
            $where[] = "TOWN_PREF_CODE = '$pref' ";
       }
       if(!empty($u_ocba)){
            $where[] = "OCBA_CODE = '$u_ocba' ";
       }
        
        if($where!=null){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = "";
        }
        
        $this->_total = $this->getTotal($where);
        $sql .= $where;

        //Order by
        $sql .= " ORDER BY TOWN_CODE DESC";
        
        //Pagination
        $sql .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
        $this->_db->setQuery($sql);
        return $this->_db->loadObjectList();
    }
    
    /**
	*Check TOWN_CODE exist
	*/
    public function checkExistID($cid){
        $qr = "SELECT TOWN_CODE FROM town WHERE TOWN_CODE = $cid";
        // load data
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();
        if(!empty($result)){ 
            return true;
        }
        return false;
    }
    
    /**
	*Insert TOWN
	*/
    public function save($data, $addNew = false){
        $table = &JTable::getInstance('bg', 'bgTable');
        $table->bind($data);
        if($table->store(false, $addNew)){
            return true;
        }
        return false;
    }
    
    /**
	*Load data TOWN for Edit
	*/
    function load($cid){
        $table = &JTable::getInstance('bg','bgTable');
        $table->load($cid);
        return $table;
    }
    
    /**
	*Delete information Town
	*/
    public function remove($cid){
        $cid = implode(',', $cid);
        $qr = "DELETE FROM town WHERE TOWN_CODE IN($cid)";
        $this->_db->setQuery($qr);
        if($this->_db->query()){
            return true;
        }
        else{
            return false;
        }
    } 
    
    /**
	*Check PREF_CODE exist
	*/
    public function getPREF_CODE($pref_Name){
        $qr = "SELECT PREF_CODE FROM pref WHERE PREF_NAME = '$pref_Name'";
        $this->_db->setQuery($qr);
        return $this->_db->loadObject();
    }
}
